<!--
 @description 经销商收货地址
 @fileName Address.vue
 @author zengqiongying
 @created 2023/04/28 13:54:59
-->
<template>
  <div class="has-foot-pb">
    <Card v-for="(item, i) in list" :key="`${i}-${item.code}`">
      <template #title>
        <div class="card-header">
          <span class="name" v-if="item.changeType !== 'B'">
            <span class="icon"></span>{{ item.code }} {{ item.name }}
          </span>
          <span class="name" v-else>
            <span class="icon"></span>收货地址{{ i + 1 }}
            <span v-if="item.changeType === 'B'" class="fw-normal ml8 color-error">(新增)</span>
          </span>
          <span v-if="item.changeType === 'B'" class="btn-del" @click="onDelete(i)"></span>
        </div>
      </template>
      <template v-if="item.changeType === 'B'">
        <Cell modelValue="新增" title="变更类型" required readonly />
      </template>

      <template v-else>
        <FieldPicker
          v-model="item.changeType"
          title="变更类型"
          placeholder="请选择"
          :url="`/data-dict/app/v2/dictionary/CTM_CHANGE_TYPE`"
          label-name="dictName"
          value-name="dictCode"
          method="get"
          :params="{ query: { appCode: 'slm', domainCode: 'marketing' }, type: 0 }"
          :excludes="['B']"
          clear
          @change="onTypeChange(i)"
        />
      </template>
      <AddressItemView v-if="!['A', 'B'].includes(item.changeType)" :modelValue="list[i]" hide-name />
      <AddressItem v-else v-model:modelValue="list[i]" :hide-name="item.changeType !== 'B'" />
    </Card>
    <Card>
      <div class="btn-add-larg" @click="onCreat">
        <qmp-icon name="plus" />
        添加新的收货地址
      </div>
    </Card>
    <div :class="['ctm-content-foot', { relative: keyboard.show }]">
      <div class="btn radius-larg ctm-btn-main" @click="onSave(true)">保存</div>
    </div>
  </div>
</template>

<script lang="ts">
  import { reactive, toRefs, getCurrentInstance, nextTick } from 'vue'
  import { useRouter, useRoute } from 'vue-router'
  import Card from '@/components/Card/index.vue'
  import Cell from '@/components/Cell/index.vue'
  import FieldPicker from '@/components/FieldPicker/index.vue'
  import AddressItem from '../components/Address/Item.vue'
  import AddressItemView from '../components/Address/ItemView.vue'
  // import { detail } from '@/apis/dealerMarketing/ArchiveChange'
  import { addressMixin } from '../mixins/Address'
  import GetKeyboardMixin from '@/utils/useKeyboard'
  const none: any[] = [null, undefined, '']
  export default {
    name: 'DealerArchiveChangeAddress',
    components: { Card, Cell, AddressItem, AddressItemView, FieldPicker },
    setup() {
      const { proxy }: any = getCurrentInstance()
      const $router = useRouter()
      const $route = useRoute()
      const { datas, getList, getMdmAddressList, onSave, onCreat, onDelete, onTypeChange } = addressMixin(() => {
        proxy.$SimpleEvents.emit('refreshCustomerChangeEditInfo', datas.id)
        nextTick(() => {
          $router.goBack()
        })
      })
      datas.listSource = [] // 原始数据
      datas.id = $route.query.id || ''
      datas.changeCode = $route.query.changeCode || ''
      datas.keyboard = {
        show: false
      }
      datas.keyboard = GetKeyboardMixin()

      /* const getDetailList = () => {
        const id = `${datas.id}`
        detail(id).then((res: any) => {
          datas.changeCode = res.data?.code || ''
          const list: any[] = [] //来源于mdm的数据
          const insertList: any[] = [] //新增部分的数据
          const dtos = res.data?.addressDTOS || []
          // 过滤掉isbefore =0，变更类型为空的数据（当isBefore =0 时，变更类型changeType必须有值）
          const useDTOS = dtos.filter((v: any) => !(v.isBefore !== 1 && !['A', 'B', 'C'].includes(v.changeType)))
          useDTOS.forEach((e: any) => {
            if (none.includes(e.code)) {
              insertList.push(e)
            } else {
              // 去重，若isbefore=1和isbefore=0 都存在，取isbefore=0的数据
              const i = list.findIndex((v) => v.code === e.code)
              if (i > -1) {
                const o = list[i]
                if (o.isBefore === 1 && e.isBefore !== 1) {
                  list[i] = e
                }
              } else {
                list.push(e)
              }
            }
          })
          // 只有isBefore===1的数据为原始数据
          datas.listSource = dtos.filter((v: any) => v.isBefore === 1)
          datas.list = [...list, ...insertList].map((v) => {
            return { ...v, ...{ changeType: v.changeType || '' } }
          })
        })
      } */
      getList()
      getMdmAddressList()

      return {
        ...toRefs(datas),
        onSave,
        onCreat,
        onDelete,
        onTypeChange
      }
    }
  }
</script>
<style lang="less" scoped>
  @import url('../styles/common.less');
</style>
